package com.vhr.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vhr.entities.Employee;
import com.vhr.entities.EmployeeExample;
import com.vhr.entities.Leave;
import com.vhr.entities.LeaveExample;
import com.vhr.mapper.DepartmentMapper;
import com.vhr.mapper.EmployeeMapper;
import com.vhr.mapper.LeaveMapper;
import com.vhr.service.ILeaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
@Cacheable(value = "leave")
public class ILeaveServiceImpl implements ILeaveService{

    @Autowired
    public LeaveMapper leaveMapper;
    @Autowired
    public DepartmentMapper departmentMapper;
    @Autowired
    public EmployeeMapper employeeMapper;

    //    管理员查询所有请假申请申请
    @Override
    public List<Leave> selectAll() {
        LeaveExample example = new LeaveExample();
        List<Leave> leaveList = leaveMapper.selectByExample(example);
        return leaveList;
    }

    //管理员查询所有未审批的请假申请
    @Override
    public List<Leave> selectWithoutJudge() {
        LeaveExample example = new LeaveExample();
        LeaveExample.Criteria criteria = example.createCriteria();
        criteria.andLeaveStatusEqualTo(0);
        List<Leave> leaveList = leaveMapper.selectByExample(example);
        return leaveList;
    }

    //    根据主键查找请假申请记录
    @Override
    public Leave selectByPrimary(Integer leaveId) {
        return leaveMapper.selectByPrimaryKey(leaveId);
    }


    //    管理员条件模糊搜索查询请假申请记录
    @Cacheable(key = "'pn-'+ #pn+ 'deptId-' + #dep + 'empName-' + #empName + 'status-' + #status")
    @Override
    public PageInfo selectCheckByCondition(Integer pn, Integer deptId, String empName, Integer status) {
        if(deptId != null && (empName != null) ){
            EmployeeExample employeeExample = new EmployeeExample();
            EmployeeExample.Criteria criteria = employeeExample.createCriteria();
            criteria.andDeptIdEqualTo(deptId);
            criteria.andEmpNameLike("%" + empName + "%");
            List<Employee> employees = employeeMapper.selectByExample(employeeExample);
            if(employees.size() == 0)
                return null;
//            List<Leave> leaveList = new ArrayList<>();
            List<Integer> ids = new ArrayList<>();
            for(Employee employee : employees){
                ids.add(employee.getEmpId());
            }
            LeaveExample leaveExample = new LeaveExample();
            LeaveExample.Criteria criteria1 = leaveExample.createCriteria();
            criteria1.andEmpIdIn(ids);
            if(status != -1)
                criteria1.andLeaveStatusEqualTo(status);
            else{
                criteria1.andLeaveStatusNotEqualTo(3);
                criteria1.andLeaveStatusNotEqualTo(4);
            }
            leaveExample.setOrderByClause("submit_time DESC");
            PageHelper.startPage(pn, 8);
            PageInfo pageInfo = new PageInfo(leaveMapper.selectByExample(leaveExample), 6);
            return pageInfo;
        }
        if(deptId != null){
            EmployeeExample employeeExample = new EmployeeExample();
            EmployeeExample.Criteria criteria = employeeExample.createCriteria();
            criteria.andDeptIdEqualTo(deptId);
            List<Employee> employees = employeeMapper.selectByExample(employeeExample);
            if(employees.size() == 0)
                return null;
            List<Integer> ids = new ArrayList<>();
            for(Employee employee : employees){
                ids.add(employee.getEmpId());
            }
            LeaveExample leaveExample = new LeaveExample();
            LeaveExample.Criteria criteria1 = leaveExample.createCriteria();
            criteria1.andEmpIdIn(ids);
            if(status != -1)
                criteria1.andLeaveStatusEqualTo(status);
            else{
                criteria1.andLeaveStatusNotEqualTo(3);
                criteria1.andLeaveStatusNotEqualTo(4);
            }
            leaveExample.setOrderByClause("submit_time DESC");
            PageHelper.startPage(pn, 8);
            PageInfo pageInfo = new PageInfo(leaveMapper.selectByExample(leaveExample), 6);
            return pageInfo;
        }
        else if(empName != null){
            EmployeeExample employeeExample = new EmployeeExample();
            EmployeeExample.Criteria criteria = employeeExample.createCriteria();
            criteria.andEmpNameLike("%" + empName + "%");
            List<Employee> employees = employeeMapper.selectByExample(employeeExample);
            List<Integer> ids = new ArrayList<>();
            for(Employee employee : employees){
                ids.add(employee.getEmpId());
            }
            LeaveExample leaveExample = new LeaveExample();
            LeaveExample.Criteria criteria1 = leaveExample.createCriteria();
            criteria1.andEmpIdIn(ids);
            if(status != -1)
                criteria1.andLeaveStatusEqualTo(status);
            else{
                criteria1.andLeaveStatusNotEqualTo(3);
                criteria1.andLeaveStatusNotEqualTo(4);
            }
            leaveExample.setOrderByClause("submit_time DESC");
            PageHelper.startPage(pn, 8);
            PageInfo pageInfo = new PageInfo(leaveMapper.selectByExample(leaveExample), 6);
            return pageInfo;
        }
        else if(deptId == null && empName == null){
            EmployeeExample employeeExample = new EmployeeExample();
            List<Employee> employees = employeeMapper.selectByExample(employeeExample);
            if(employees.size() == 0)
                return null;
            List<Integer> ids = new ArrayList<>();
            for(Employee employee : employees){
                ids.add(employee.getEmpId());
            }
            LeaveExample leaveExample = new LeaveExample();
            LeaveExample.Criteria criteria1 = leaveExample.createCriteria();
            criteria1.andEmpIdIn(ids);
            if(status != -1)
                criteria1.andLeaveStatusEqualTo(status);
            else{
                criteria1.andLeaveStatusNotEqualTo(3);
                criteria1.andLeaveStatusNotEqualTo(4);
            }
            leaveExample.setOrderByClause("submit_time DESC");
            PageHelper.startPage(pn, 8);
            PageInfo pageInfo = new PageInfo(leaveMapper.selectByExample(leaveExample), 6);
            return pageInfo;
        }
        return null;
    }


    //根据员工外键查询员工请假申请信息
    @Override
    @Cacheable(key = "#emp.empId")
    public List<Leave> selectByEmployee(Employee emp) {
        LeaveExample example = new LeaveExample();
        LeaveExample.Criteria criteria = example.createCriteria();
        criteria.andEmpIdEqualTo(emp.getEmpId());
        example.setOrderByClause("submit_time DESC");
        List<Leave> leaveList = leaveMapper.selectByExample(example);
        return leaveList;
    }

    //	插入请假信息（申请请假）
    @Override
    public int addLeave(Leave leave) {
        return leaveMapper.insertSelective(leave);
    }

    //管理员审批请假
    @Override
    public int judegLeave(Leave leave) {
        //leave.setLeaveStatus(1);
        return leaveMapper.updateByPrimaryKeySelective(leave);
    }

    //管理员根据主键更改信息
    @Override
    @CachePut(key = "#leave.leaveId")
    public int updateByPrimary(Leave leave) {
        return leaveMapper.updateByPrimaryKeySelective(leave);
    }

    //管理员根据主键删除信息
    @Override
    @CacheEvict(key = "#leave.leaveId")
    public int deleteByPrimary(Leave leave) {
       Leave l = leaveMapper.selectByPrimaryKey(leave.getLeaveId());
       int status = l.getLeaveStatus();
       if(status == 1)
           l.setLeaveStatus(3);
       else if (status == 2)
           l.setLeaveStatus(4);
        return leaveMapper.updateByPrimaryKeySelective(l);
    }

    //    员工条件查看请假审批是否通过

    @Override
    @Cacheable(key = "'empId-'+#empId + 'status-'+#status")
    public List<Leave> selectLeaveAllowNot(Integer empId, Integer status) {
        LeaveExample example = new LeaveExample();
        LeaveExample.Criteria criteria = example.createCriteria();
        criteria.andEmpIdEqualTo(empId);
        if(status != -1){
            criteria.andLeaveStatusEqualTo(status);
            if(status == 1)
                example.or().andLeaveStatusEqualTo(3);
            else if(status == 2)
                example.or().andLeaveStatusEqualTo(4);
        }
        example.setOrderByClause("submit_time DESC");

        return leaveMapper.selectByExample(example);
    }
}
